package cn.itcast.classSM.service.Impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.classSM.dao.AdministratorRepository;
import cn.itcast.classSM.domain.CheckMessage;
import cn.itcast.classSM.domain.Clazz;
import cn.itcast.classSM.domain.MainCheckMessage;
import cn.itcast.classSM.service.AdministratorService;
@Service
@Transactional
public class AdministratorServiceImpl implements AdministratorService{
    @Autowired
    private AdministratorRepository administratorRepository;
	@Override
	public void add(Clazz clazz) {
		administratorRepository.save(clazz);
		
	}

	@Override
	public void delete(Clazz clazz) {
		administratorRepository.update(clazz.getCid());
		
	}

	@Override
	public List<Clazz> findAll() {
		
		return administratorRepository.findAll();
	}

	@Override
	public List<CheckMessage> findByMonth(String checkTime) throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		Date startDate = format.parse(checkTime);
		Calendar rightNow = Calendar.getInstance();    
	    rightNow.setTime(startDate);    
	    rightNow.add(Calendar.MONTH, 1);    
	    Date endDate = rightNow.getTime(); 
		List<CheckMessage> list = administratorRepository.findMonth(startDate,endDate);
		return list;
	}

	@Override
	public List<MainCheckMessage> findByClazzMonth(String checkTime, Integer cid) throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		Date date = format.parse(checkTime);
		List<MainCheckMessage> list = administratorRepository.findClazzMonth(date, cid);
		return list;
	}

}
